package action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import basic.SortInfo;
import common.BaseAction;
import common.dict.CommonDict;
import entities.TCurrency;
import interfaces.ICurrencyService;
import util.CurrencyUtil;
import util.ParameterUtil;
import utils.DateUtil;
import utils.ExcelUtil;
import utils.StringUtil;

public class CurrencyAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7507534255609348887L;

	private ICurrencyService currencyService;

	private TCurrency currencyBean;
	private List<TCurrency> currencyList;

	private String curNo;
	private String curNos;
	private String curName;
	private Double curRate;
	private String curState;
	private Date curDate;
	private Date curDateTop;
	private Date curDateEnd;
	private String curRemark;

	// 展示对象
	public String show() throws Exception {
		if (!currencyService.exist(curNo)) {
			addActionError("数据不存在");
			return INFO;
		} else {
			currencyBean = currencyService.fetch(curNo);
			return SUCCESS;
		}
	}

	// 编辑对象
	public String edit() throws Exception {
		if (!currencyService.exist(curNo)) {
			currencyBean = new TCurrency();
		} else {
			currencyBean = currencyService.fetch(curNo);
		}
		return SUCCESS;
	}

	// 持久化对象
	public String save() throws Exception {
		if (!currencyService.exist(curNo)) {
			currencyBean = new TCurrency();
		} else {
			currencyBean = currencyService.fetch(curNo);
		}
		currencyBean.setCurNo(curNo);
		currencyBean.setCurName(curName);
		currencyBean.setCurRate(curRate);
		currencyBean.setCurState(curState);
		currencyBean.setCurDate(new Date());
		currencyBean.setCurRemark(curRemark);

		currencyService.persist(currencyBean);
		return SUCCESS;
	}

	// 持久化对象
	public String saverate() throws Exception {
		if (!currencyService.exist(curNo)) {
			addActionError("数据不存在");
			return INFO;
		}
		currencyBean = currencyService.fetch(curNo);
		currencyBean.setCurRate(curRate);
		currencyBean.setCurDate(new Date());
		currencyService.persist(currencyBean);
		return SUCCESS;
	}

	// 批量更新
	public String savebatch() throws Exception {
		if (StringUtil.isEmpty(curNos)) {
			addActionError("未选中任何要更新的货币");
			return INFO;
		}
		for (String curNo : StringUtil.split(curNos)) {
			if (!currencyService.exist(curNo)) {
				currencyBean = new TCurrency();
			} else {
				currencyBean = currencyService.fetch(curNo);
			}
			try {
				currencyBean.setCurRate(new Double(CurrencyUtil.lookup(currencyBean.getCurNo())));
				currencyBean.setCurDate(new Date());
				currencyService.persist(currencyBean);
				addActionError("<font color='green'>更新成功</font> ["+currencyBean.getCurNo()+"]"+currencyBean.getCurName());
			} catch (Exception e) {
				addActionError("<font color='red'>更新失败</font> ["+currencyBean.getCurNo()+"]"+currencyBean.getCurName());
			}
		}
		return SUCCESS;
	}

	public String list() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("curNo", StringUtil.split(curNo));
		paramMap.put("curDate", ParameterUtil.dateCombin(curDateTop, curDateEnd));

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "curDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(pageIndex, 100));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TCurrency> resultSet = currencyService.search(querySet);
		currencyList = resultSet.getResultList();
		pageInfo = resultSet.getPageInfo();
		return SUCCESS;
	}

	// 导出数据 查询条件等同于列表查询条件
	public String export() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("curNo", StringUtil.split(curNo));
		paramMap.put("curDate", ParameterUtil.dateCombin(curDateTop, curDateEnd));

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "curDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(1, CommonDict.EXPORT_LIMIT));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TCurrency> resultSet = currencyService.search(querySet);
		currencyList = resultSet.getResultList();

		List<String> fieldHead = new ArrayList<String>();
		fieldHead.add("编号");
		fieldHead.add("货币名称");
		fieldHead.add("汇率");
		fieldHead.add("是否可用");
		fieldHead.add("日期日期");
		fieldHead.add("日期时间");
		fieldHead.add("备注");

		List<List<String>> fieldData = new ArrayList<List<String>>();

		for (TCurrency currencyBean : currencyList) {
			List<String> fieldItem = new ArrayList<String>();
			fieldItem.add(currencyBean.getCurNo());
			fieldItem.add(currencyBean.getCurName());
			fieldItem.add(currencyBean.getCurRate().toString());
			fieldItem.add((currencyBean.getCurState().equals("0")) ? "无效" : "有效");
			fieldItem.add(DateUtil.format(currencyBean.getCurDate(), DateUtil.DATE));
			fieldItem.add(DateUtil.format(currencyBean.getCurDate(), DateUtil.TIME));
			fieldItem.add(currencyBean.getCurRemark());

			fieldData.add(fieldItem);
		}

		inputStream = ExcelUtil.excel(fieldHead, fieldData);
		return SUCCESS;
	}

	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////

	public ICurrencyService getCurrencyService() {
		return currencyService;
	}

	public void setCurrencyService(ICurrencyService currencyService) {
		this.currencyService = currencyService;
	}

	public TCurrency getCurrencyBean() {
		return currencyBean;
	}

	public void setCurrencyBean(TCurrency currencyBean) {
		this.currencyBean = currencyBean;
	}

	public List<TCurrency> getCurrencyList() {
		return currencyList;
	}

	public void setCurrencyList(List<TCurrency> currencyList) {
		this.currencyList = currencyList;
	}

	public String getCurNo() {
		return curNo;
	}

	public void setCurNo(String curNo) {
		this.curNo = curNo;
	}

	public String getCurNos() {
		return curNos;
	}

	public void setCurNos(String curNos) {
		this.curNos = curNos;
	}

	public String getCurName() {
		return curName;
	}

	public void setCurName(String curName) {
		this.curName = curName;
	}

	public Double getCurRate() {
		return curRate;
	}

	public void setCurRate(Double curRate) {
		this.curRate = curRate;
	}

	public String getCurState() {
		return curState;
	}

	public void setCurState(String curState) {
		this.curState = curState;
	}

	public Date getCurDate() {
		return curDate;
	}

	public void setCurDate(Date curDate) {
		this.curDate = curDate;
	}

	public Date getCurDateTop() {
		return curDateTop;
	}

	public void setCurDateTop(Date curDateTop) {
		this.curDateTop = curDateTop;
	}

	public Date getCurDateEnd() {
		return curDateEnd;
	}

	public void setCurDateEnd(Date curDateEnd) {
		this.curDateEnd = curDateEnd;
	}

	public String getCurRemark() {
		return curRemark;
	}

	public void setCurRemark(String curRemark) {
		this.curRemark = curRemark;
	}

}
